package com.group.itms.routes.adapters;

import java.util.List;

import com.group.itms.routes.utils.ListViewItem;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class MultiViewTypeAdapter extends ArrayAdapter<ListViewItem> {

	public static final int VIEW_TYPE_HEADER = 0;
	public static final int VIEW_TYPE_ITEM = 1;

	private int mViewTypeCount;
	private LayoutInflater mInflater;

	public MultiViewTypeAdapter(Context context) {
		this(context, 2);
	}

	public MultiViewTypeAdapter(Context context, int viewTypeCount) {
		super(context, 0);
		if (viewTypeCount < 0) {
			throw new IllegalArgumentException("viewTypeCount < 0");
		}
		mViewTypeCount = viewTypeCount;
		mInflater = LayoutInflater.from(context);
	}

	public void addAll(List<ListViewItem> items) {
		setNotifyOnChange(false);
		clear();
		for (ListViewItem item : items) {
			add(item);
		}
		setNotifyOnChange(true);
		notifyDataSetChanged();
	}

	@Override
	public boolean isEnabled(int position) {
		return getItem(position).isClickable();
	}
	
	public Object getItemData(int position) {
		ListViewItem item = getItem(position);
		return item != null ? item.getData() : null;
	}
	
	@Override
	public int getViewTypeCount() {
		return mViewTypeCount;
	}

	@Override
	public int getItemViewType(int position) {
		return getItem(position).getViewType();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return getItem(position).getView(mInflater, convertView);
	}

}
